//
//  CalculatorView.swift
//  HengYouCai
//
//  Created by lwj on 2017/11/14.
//  Copyright © 2017年 lwj. All rights reserved.
//

import UIKit

class CalculatorView: UIView , UITextFieldDelegate{
   
    
    var calculater :UIView!
    

    init( _ productID: String, _ profit: String,  _  limitValue: String,model:DetailModel?=nil) {
        //
        super.init(frame: UIScreen.main.bounds)
        self.backgroundColor = UIColor(white: 0, alpha: 0.35)
       
        
        weak var weakSelf = self
        //        self.alpha = 0.35
        let myWindow  = UIApplication.shared.keyWindow
        myWindow?.addSubview(self)
        
        calculater = Bundle.main.loadNibNamed("CalculatorView", owner: nil, options: nil)?.first as? UIView
        calculater.layer.cornerRadius = 5
        calculater.clipsToBounds  = true
        
        self.addSubview(calculater)
        
        //文案 赋值
        let titleLabel:UILabel = calculater.viewWithTag(101) as! UILabel
        titleLabel.text = model?.calculatorTitle
        
        let calculatorProfitLabel:UILabel = calculater.viewWithTag(102) as! UILabel
        calculatorProfitLabel.text = model?.calculatorProfitLabel
        
        let calculatorPlstimeLimitLabel:UILabel = calculater.viewWithTag(103) as! UILabel
        calculatorPlstimeLimitLabel.text = model?.calculatorPlstimeLimitLabel
        
        let bottomLable:UILabel = calculater.viewWithTag(104) as! UILabel
        bottomLable.text = model?.calculatorResultLabel
        
        
        let bgView = calculater.viewWithTag(1004)
        bgView?.layer.borderColor = Normal_888Color.cgColor
        bgView?.layer.borderWidth = 0.5
        
        let profitLab = calculater.viewWithTag(1002) as? UILabel
        profitLab?.text = profit
        
        
        let limitValueLab = calculater.viewWithTag(1003) as? UILabel
        limitValueLab?.text = limitValue
        
        let calculateProfitLab = calculater.viewWithTag(1005) as? UILabel
        //        limitValueLab?.text = limitValue
        let calBtn = calculater.viewWithTag(1004)?.viewWithTag(10001) as? UIButton
//        calBtn?.titleLabel?.text = model.calculatorButtonLabel //按钮文字
        calBtn?.setTitle(model?.calculatorButtonLabel, for: .normal)
        
        let valueTF = calculater.viewWithTag(1004)?.viewWithTag(10002) as? UITextField
        valueTF?.placeholder = model?.calculatorAmountTips //输入框
        
        valueTF?.delegate = self
        calBtn?.rac_signal(for: UIControlEvents.touchUpInside).subscribeNext({ (_) in
            
            let amountStr : String = (valueTF?.text)!
            
            HYCNetService.productsCalculationProfit(withDict: ["id":productID,"amount":amountStr], onSuccess: { (result) in
                //
                let  json = JSON(result as Any)
                let calculateStr = String(format: "%.2f",json["profit"].floatValue)
                calculateProfitLab?.text = calculateStr
            }, andFailed: { (code, msg) in
                //
            })
        })
        
        let cancleBtn = calculater.viewWithTag(1001) as! UIButton
        cancleBtn.titleLabel?.text = model?.calculatorButtonLabel
        cancleBtn.rac_signal(for: UIControlEvents.touchUpInside).subscribeNext { (_) in
            weakSelf?.removeFromSuperview()
        }
        
        _ = calculater.sd_layout().centerXEqualToView(self)?.centerYEqualToView(self)?.leftSpaceToView(self,33)?.rightSpaceToView(self,33)?.heightIs(200)
        
        NotificationCenter.default.rac_addObserver(forName: "UIKeyboardWillShowNotification", object: nil).subscribeNext { (noti) in
            //
            let userInfo = noti?.userInfo
            let keyBoardBounds : CGRect = userInfo!["UIKeyboardBoundsUserInfoKey"] as! CGRect
            let height = keyBoardBounds.size.height
            
            
            weakSelf?.calculater.center = CGPoint(x: JCWSCREENW/2, y: (JCWSCREENH-height)/2)
            
        }
        NotificationCenter.default.rac_addObserver(forName: "UIKeyboardWillHideNotification", object: nil).subscribeNext { (noti) in
            //
            
            weakSelf?.calculater.center = CGPoint(x: JCWSCREENW/2, y: JCWSCREENH/2)
            
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }
    
    
    
    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        
        if string == "" {
            let calculateProfitLab = calculater.viewWithTag(1005) as? UILabel
            calculateProfitLab?.text = "0.00"
        }
        if range.location > 10 {
            return false
        }
        return true
        
    }
    
    
    
    
}

